package freespiritsocial.io.user.decoration

import android.annotation.SuppressLint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class SpaceItemDecoration(
    private val leftRight: Int,
    private val topBottom: Int,
    private val firstDim: Int = 0,
    private val firstNeedTop: Boolean = true,
    private val headerCount: Int = 0
): RecyclerView.ItemDecoration() {

    @SuppressLint("WrongConstant")
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val layoutManager = parent.layoutManager as? LinearLayoutManager
        if (layoutManager is GridLayoutManager) {
            //最后一项需要 bottom
            if (parent.getChildAdapterPosition(view) == layoutManager.itemCount - 1) {
                outRect.right = leftRight
            }
            if (!firstNeedTop && parent.getChildAdapterPosition(view) == headerCount) {
                outRect.top = 0
            } else {
                outRect.top = topBottom
            }
            outRect.left = leftRight

            if (headerCount > 0 && parent.getChildAdapterPosition(view) < headerCount) {
                outRect.left = 0
                outRect.right = 0
            }
            return
        }
        //竖直方向的
        if (layoutManager!!.orientation == LinearLayoutManager.VERTICAL) {
            //最后一项需要 bottom
            if (parent.getChildAdapterPosition(view) == layoutManager.itemCount - 1) {
                outRect.bottom = topBottom
            }
            if (!firstNeedTop && parent.getChildAdapterPosition(view) == 0) {
                outRect.top = 0
            } else {
                outRect.top = topBottom
            }
            outRect.left = leftRight
            outRect.right = leftRight
        } else {
            //最后一项需要right
            if (parent.getChildAdapterPosition(view) == layoutManager.itemCount - 1) {
                outRect.right = leftRight
            }
            outRect.top = topBottom
            if (firstNeedTop && parent.getChildAdapterPosition(view) == 0) {
                outRect.left = firstDim
            } else {
                outRect.left = leftRight

            }
            outRect.bottom = topBottom
        }
        if (headerCount > 0 && parent.getChildAdapterPosition(view) < headerCount) {
            outRect.left = 0
            outRect.right = 0
        }
    }


}

